<template>
  <div ref="chart" style="height:300px;width:100%" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: ['checkGoodsLine'],
  data() {
    return {
      chart: null,
      seriesData: []
    }
  },
  watch: {
    checkGoodsLine: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (!this.$refs.chart) {
        return;
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.checkGoodsLine.map(o => {
        this.seriesData.push({
          name: o.goodsName,
          type: 'line',
          stack: 'Total',
          data: o.data
        });
      })
      this.chart.setOption(
        {
          xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: this.checkGoodsLine.map(o => o.goodsName),
              type: 'line',
              smooth: true
            }
          ]
        })
    }
  }
}
</script>
